#coding:utf-8
from handlers.base.base_handler import BaseHandler
from libs.account.account_auth_libs import (create_captcha_img,
                                            auth_captcha,login,
                                            get_mobile_code_lib,
                                            regist,
                                            add_avatar_lib,
                                            logout,
                                            )
class CapchaHandler(BaseHandler):
    def get(self):
        pre_code=self.get_argument('pre_code','')
        code=self.get_argument('code','')
        img=create_captcha_img(self,pre_code,code)
        self.set_header('Content-Type','image/jpg')
        self.write(img)


class LoginHandler(BaseHandler):
    def get(self):
        self.render('account/auth_login.html')

    def post(self):
        name=self.get_argument('name','')
        password=self.get_argument('password','')
        code=self.get_argument('code','')
        captcha_code=self.get_argument('captcha','')
        result=auth_captcha(self,captcha_code,code)
        if result['status'] is False:
            return self.write({'status':400,'msg':result['msg']})
        result= login(self,name,password)
        if result['status'] is True:
            return self.write({'status':200,'msg':result['msg']})
        return self.write({'status':400,'msg':result['msg']})

class LogoutHandler(BaseHandler):
    def get(self):
        logout(self)
        print u'已退出登陆'
        return self.redirect('/auth/user_login')


class RegistHandler(BaseHandler):
    def get(self):
        self.render('account/auth_regist.html',message='用户注册')
    def post(self):
        mobile=self.get_argument('mobile','')
        mobile_captcha=self.get_argument('mobile_captcha','')
        code=self.get_argument('code','')
        name=self.get_argument('name','')
        password1=self.get_argument('password1','')
        password2=self.get_argument('password2','')
        captcha=self.get_argument('captcha','')
        result=regist(self,name,mobile,mobile_captcha,
                      password1,password2,captcha,code)
        if result['status'] is True:
            self.write({'status':200,'msg':result['msg']})
            return self.redirect('/auth/user_login')
        return self.write({'status':400,'msg':result['msg']})


class MobileCodeHandler(BaseHandler):
    def post(self):
        mobile=self.get_argument('mobile','')
        code=self.get_argument('code','')
        captcha=self.get_argument('captcha','')
        print mobile,code,captcha
        result=get_mobile_code_lib(self,mobile,code,captcha)
        if result['status'] is True:
            return self.write({'status':200,'msg':result['msg']})
        return self.write({'status':400,'msg':result['msg']})


class ProfileAddAvatarHandler(BaseHandler):
    def post(self):
        avatar_data=self.request.files.get('user_avatar','')
        #[{'body': '123 \xb9\xfe', 'content_type': u'text/plain', 'filename': u'aa.txt'}]
        #print avatar_data
        result= add_avatar_lib(self,avatar_data[0]['body'])
        if result['status'] is True:
            return self.redirect('/account/user_edit')
        return self.write(result['msg'])